//
//  704. 二分查找.swift
//  Top600
//
//  Created by rjb on 2021/8/9.
//

import Foundation

class Solution704 {
    // 有序数组很容易就想到利用二分查找
    func search(_ nums: [Int], _ target: Int) -> Int {
        var left = 0
        var right = nums.count - 1
        while left <= right {
            let mid = (left + right) / 2
            if nums[mid] < target {
                left += 1
            } else if nums[mid] > target {
                right -= 1
            } else {
                return mid
            }
        }
        return -1
    }
    static func test() {
        let solution = Solution704()
        let nums = [-1,0,3,5,9,12]
        let target = 9
        let result = solution.search(nums, target)
        print(result)
    }
}
